using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Enemy : MonoBehaviour
{
    public float hitSpeed;  //受击后退速度
    public int health;
    public GameObject damageFont;
    private TextMesh _textMesh;
    private Rigidbody2D _rigidbody2D;
    private Animator _animator;
    private Animator splashAnimator;
    private bool isHit = false;
    
    // Start is called before the first frame update
    void Start()
    {
        _rigidbody2D = gameObject.GetComponent<Rigidbody2D>();
        _animator = gameObject.GetComponent<Animator>();
        splashAnimator = transform.GetChild(0).GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void Hit(float foward, int damage, int combo)
    {
        if (!isHit)
        {
            isHit = true;
            transform.localScale = new Vector3(-foward, 1, 1);  //受击时朝向player
            _animator.SetTrigger("Hit");                        //播放受击动画
            splashAnimator.SetTrigger("HitSplash");             //播放受击特效动画
            _rigidbody2D.velocity = new Vector2(foward*hitSpeed, _rigidbody2D.velocity.y);//受击时像后移动
            ShowHitText(damage, combo);                             //显示受击伤害
            health -= damage;
            if (health<=0)
            {
                _animator.SetTrigger("Dead");
            }
        }
    }

    //受击动画结束关键帧调用
    private void HitOver()
    {
        isHit = false;
    }

    private void Death()
    {
        Destroy(gameObject);
    }

    private void ShowHitText(int damage, int combo)
    {
        Vector2 showPos = new Vector2(transform.position.x, transform.position.y+(float)0.71);
        GameObject gb = GameObject.Instantiate(damageFont, showPos, Quaternion.identity);
        _textMesh = gb.transform.GetChild(0).GetComponent<TextMesh>();  //字体为子物体
        _textMesh.text = damage.ToString();
        if (combo == 3)
            _textMesh.color = Color.red;
    }

}
